package test1.library.command;

import java.util.ArrayList;
import java.util.List;

public class CommandsSingleton {

	private static CommandsSingleton instance;

	private List<ICommand> listCommand;

	private CommandsSingleton() {
		listCommand = new ArrayList<ICommand>();
		listCommand.add(new addbooks());
		listCommand.add(new Listbooks());
		listCommand.add(new search());
                listCommand.add(new delete());
	        listCommand.add(new addinfo());                
	        listCommand.add(new exit());
                System.out.println("Constructor Created");
	}

	public synchronized static CommandsSingleton getInstance() {
		if (instance == null) {
			instance = new CommandsSingleton();
		}
		return instance;
	}

	public List<ICommand> getCommands() {
		return listCommand;
	}

	public String execute(String... args) {
		if (args != null && args.length > 0) {
			String commandName = args[0];
			for (ICommand command : listCommand) {
				if (commandName.equals(command.name())) {
					command.execute(args);
					return "success";
				}
			}
			return "ERROR: command not found";
		} else {
			return "ERROR: wrong args";
		}
	}
}
